package com.unbox.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.unbox.admin.entity.RoleMenu;
import com.unbox.admin.service.IRoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import com.unbox.admin.entity.Role;
import com.unbox.admin.mapper.RoleMapper;
import com.unbox.admin.service.IRoleService;
import com.unbox.common.table.UnBoxPage;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * <角色业务实现类
 * </p>
 *
 * @author 王振宇
 * @since 2021-03-01
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Autowired
    IRoleMenuService roleMenuService;

    @Override
    public Role get(Role role) {
        LambdaQueryWrapper<Role> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (!ObjectUtils.isEmpty(role.getId())) {
            lambdaQueryWrapper.eq(Role::getId, role.getId());
        }
        if (!StringUtils.isEmpty(role.getName())) {
            lambdaQueryWrapper.eq(Role::getName, role.getName());
        }
       return super.getOne(lambdaQueryWrapper);
    }

    @Override
    public List<Role> getList(Role role) {
        LambdaQueryWrapper<Role> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (!ObjectUtils.isEmpty(role.getId())) {
            lambdaQueryWrapper.eq(Role::getId, role.getId());
        }
        if (!StringUtils.isEmpty(role.getName())) {
            lambdaQueryWrapper.like(Role::getName, role.getName());
        }
        return super.list(lambdaQueryWrapper);
    }

    @Override
    public UnBoxPage<List<Role>> getPageList(Role role, int pageNo, int pageSize) {
        LambdaQueryWrapper<Role> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (!ObjectUtils.isEmpty(role.getId())) {
            lambdaQueryWrapper.eq(Role::getId, role.getId());
        }
        if (!StringUtils.isEmpty(role.getName())) {
            lambdaQueryWrapper.like(Role::getName, role.getName());
        }
        lambdaQueryWrapper.orderByDesc(Role::getCreateTime);
        Page<Role> page = new Page<>(pageNo,pageSize);
        IPage<Role> pageResult = this.baseMapper.selectPage(page, lambdaQueryWrapper);
        UnBoxPage<List<Role>> unBoxPage = new UnBoxPage<>();
        unBoxPage.setPageNo(pageNo);
        unBoxPage.setPageSize(pageSize);
        unBoxPage.setSumPageCount(pageResult.getPages());
        unBoxPage.setSumDataCount(pageResult.getTotal());
        unBoxPage.setDatas(pageResult.getRecords());
        return unBoxPage;
    }

    @Override
    public List<Role> getIdsList(List<Integer> ids) {
        LambdaQueryWrapper<Role> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(Role::getId, ids);
        return super.list(lambdaQueryWrapper);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean save(Role entity) {
        super.save(entity);
        if (!ObjectUtils.isEmpty(entity.getRoleMenus()) && entity.getRoleMenus().size() > 0) {
            List<RoleMenu> roleMenus = new ArrayList<>();
            entity.getRoleMenus().forEach(menu -> {
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setMenuId(menu.getMenuId());
                roleMenu.setRoleId(entity.getId());
                roleMenus.add(roleMenu);
            });
            roleMenuService.saveBatch(roleMenus);
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean saveBatch(List<Role> entityList) {
        return super.saveBatch(entityList);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean update(Role entity) {
        // 删除菜单 以及 权限关联表
        LambdaQueryWrapper<RoleMenu> roleMenuLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roleMenuLambdaQueryWrapper.eq(RoleMenu::getRoleId, entity.getId());
        roleMenuService.remove(roleMenuLambdaQueryWrapper);

        if (!ObjectUtils.isEmpty(entity.getRoleMenus()) && entity.getRoleMenus().size() > 0) {
            List<RoleMenu> roleMenus = new ArrayList<>();
            entity.getRoleMenus().forEach(menu -> {
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setMenuId(menu.getMenuId());
                roleMenu.setRoleId(entity.getId());
                roleMenus.add(roleMenu);
            });
            roleMenuService.saveBatch(roleMenus);
        }
        LambdaQueryWrapper<Role> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Role::getId, entity.getId());
        return super.update(entity, lambdaQueryWrapper);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean updateBatchById(List<Role> entityList) {
        return super.updateBatchById(entityList);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean remove(Role role) {
        LambdaQueryWrapper<Role> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Role::getId, role.getId());

        // 删除菜单 以及 权限关联表
        LambdaQueryWrapper<RoleMenu> roleMenuLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roleMenuLambdaQueryWrapper.eq(RoleMenu::getRoleId, role.getId());
        roleMenuService.remove(roleMenuLambdaQueryWrapper);

        return super.remove(lambdaQueryWrapper);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean removeByIds(List<Integer> idList) {
        // 删除菜单 以及 权限关联表
        LambdaQueryWrapper<RoleMenu> roleMenuLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roleMenuLambdaQueryWrapper.in(RoleMenu::getRoleId, idList);
        roleMenuService.remove(roleMenuLambdaQueryWrapper);
        return super.removeByIds(idList);
    }
}
